

function addMaterialToStockInStorage() {

	//得到要添加记录的表格---tbody
	var mbody = document.getElementById("mbody");
	
	//向表格中增加一行记录
	var tr = mbody.insertRow(mbody.rows.length);
	
	//表示行的下标
	var rowIndex = mbody.rows.length - 1;
	
	//增加一个单元格
	var cell = tr.insertCell(0);	
	//复选框
	cell.innerHTML = "<td><input type='hidden' name='stockInStorageDetail' id='stockInStorageDetail" + rowIndex + "'><input type='checkbox' name='checkbox'></td>";
	
	cell = tr.insertCell(1);	
	//物料代码
	cell.innerHTML = "<td><input type='hidden' name='materialId' id='materialId" + rowIndex + "'><input type='text' name='' size='10' ondblclick='openWinMaterialForStockInStorage("+rowIndex+",800,600,1)' id='materialCode" + rowIndex + "'></td>";
	
	cell = tr.insertCell(2);	
	//物料名称
	cell.innerHTML = "<td><input type='text' name='' size='10' readonly='true' id='materialName" + rowIndex + "'></td>";
	
	cell = tr.insertCell(3);	
	//物料规格型号
	cell.innerHTML = "<td><input type='text' name='' size='10' readonly='true' id='spec" + rowIndex + "'></td>";
	
	cell = tr.insertCell(4);	
	//收料仓库
	cell.innerHTML = "<td><input type='hidden' name='storageId' id='storageId" + rowIndex + "'><input type='text' name='' size='10' ondblclick='openWinStorageInDetail(" + rowIndex + ", 800, 600, 1)' id='storageName" + rowIndex + "'></td>";
	
	cell = tr.insertCell(5);	
	//批号
	cell.innerHTML = "<td><input type='text' size='10' name='materialNO' id='materialNO" + rowIndex + "'></td>";
	
	cell = tr.insertCell(6);	
	//采购单位
	cell.innerHTML = "<td><input type='hidden' name='measureUnitId' id='measureUnitId" + rowIndex + "'><input type='text' name='' size='10' ondblclick='openWinMeasureUnit(" + rowIndex + ",800,600,0)' id='measureUnitName" + rowIndex + "'></td>";
	
	cell = tr.insertCell(7);	
	//基本单位
	cell.innerHTML = "<td><input type='text' size='10' name='' readonly='true' id='basicMeasureUnitName" + rowIndex + "'></td>";
	
	cell = tr.insertCell(8);
	//单价
	cell.innerHTML = "<td><input type='text' size='8' name='unitPrice' id='unitPrice" + rowIndex + "' onblur='getBasicFactAmount(" + rowIndex + ")'></td>";
	
	cell = tr.insertCell(9);	
	//应收数量
	cell.innerHTML = "<td><input type='text' size='10' name='shouldAmount' id='shouldAmount" + rowIndex + "' onblur='getBasicShouldAmount(" + rowIndex + ")'></td>";
	
	cell = tr.insertCell(10);	
	//基本单位应收数量
	cell.innerHTML = "<td><input type='text' size='10' name='basicShouldAmount' id='basicShouldAmount" + rowIndex + "'></td>";
	
	cell = tr.insertCell(11);
	//实收数量
	cell.innerHTML = "<td><input type='text' size='10' name='factAmount' id='factAmount" + rowIndex + "' onblur='getBasicFactAmount(" + rowIndex + ")'></td>";
	
	cell = tr.insertCell(12);
	//基本单位实收数量
	cell.innerHTML = "<td><input type='text' size='10' name='basicFactAmount' id='basicFactAmount" + rowIndex + "'></td>";
	
	cell = tr.insertCell(13);
	//金额
	cell.innerHTML = "<td><input type='text' size='5' name='money' id='money" + rowIndex + "'></td>";
	
	cell = tr.insertCell(14);
	//备注
	cell.innerHTML = "<td><input type='text' size='15' name='remark' id='remark" + rowIndex + "'></td>";
	
	cell = tr.insertCell(15);
	//采购日期
	cell.innerHTML = "<td><input type='text' size='10' name='inStorageDate' style='cursor:hand' id='inStorageDate" + rowIndex + "' onclick='time.setHook1(this)'></td>";
	
	cell = tr.insertCell(16);
	//保质期
	cell.innerHTML = "<td><input type='text' size='5' name='inStoreLife' id='inStoreLife" + rowIndex + "' onblur='countDate(" + rowIndex + ")'></td>";
	
	cell = tr.insertCell(17);
	//有效期至
	cell.innerHTML = "<td><input type='text' size='10' name='validationDate' style='cursor:hand' id='validationDate" + rowIndex + "' readonly='true'></td>";
	
	cell = tr.insertCell(18);
	//源单单号
	cell.innerHTML = "<td><input type='text' size='10' name='sourceBillCode' id='sourceBillCode" + rowIndex + "'></td>";
	
	cell = tr.insertCell(19);
	//订单单号
	cell.innerHTML = "<td><input type='text' size='10' name='sourceOrderCode' id='sourceOrderCode" + rowIndex + "'></td>";
	

}


function openWinMaterialForStockInStorage(index, width, height, scroll) {
	var truelink = "selectMaterialForStockInStorage.action?rowIndex="+index;
	openWin(truelink, null, width, height, scroll);
}


function childToParentForMaterialOfStockInStorage(index, pMaterialId, cMaterialId, pMaterialCode, cMaterialCode, pMaterialName, cMaterialName, pSpec, cSpec, pDepartmentId, cDepartmentId, pDepartmentName, cDepartmentName, pStockMeasureUnitId, cStockMeasureUnitId, pStockMeasureUnitName, cStockMeasureUnitName, pBasicMeasureUnitName, cBasicMeasureUnitName) {
		var parentMaterialId = window.opener.document.getElementById(pMaterialId + index);
		parentMaterialId.value = cMaterialId;
		
		var parentMaterialCode = window.opener.document.getElementById(pMaterialCode + index);
		parentMaterialCode.value = cMaterialCode;
		
		var parentMaterialName = window.opener.document.getElementById(pMaterialName + index);
		parentMaterialName.value = cMaterialName;
		
		var parentSpec = window.opener.document.getElementById(pSpec + index);
		parentSpec.value = cSpec;
		
		var parentDepartmentId = window.opener.document.getElementById(pDepartmentId + index);
		parentDepartmentId.value = cDepartmentId;
		
		var parentDepartmentName = window.opener.document.getElementById(pDepartmentName + index);
		parentDepartmentName.value = cDepartmentName;
		
		var parentStockMeasureUnitId = window.opener.document.getElementById(pStockMeasureUnitId + index);
		parentStockMeasureUnitId.value = cStockMeasureUnitId;
		
		var parentStockMeasureUnitName =  window.opener.document.getElementById(pStockMeasureUnitName + index);
		parentStockMeasureUnitName.value = cStockMeasureUnitName;
		
		var parentBasicMeasureUnitName = window.opener.document.getElementById(pBasicMeasureUnitName + index);
		parentBasicMeasureUnitName.value = cBasicMeasureUnitName;
		
		var parentUnitPrice = window.opener.document.getElementById('unitPrice' + index);
		parentUnitPrice.value = '0.00';
		
		var parentMoney = window.opener.document.getElementById('money' + index);
		parentMoney.value = '0.00';
		
		var parentShouldAmount = window.opener.document.getElementById('shouldAmount' + index);
		parentShouldAmount.value = '0.00';
		
		var parentBasicShouldAmount = window.opener.document.getElementById('basicShouldAmount' + index);
		parentBasicShouldAmount.value = '0.00';
		
		var parentFactAmount = window.opener.document.getElementById('factAmount' + index);
		parentFactAmount.value = '0.00';
		
		var parentBasicFactAmount = window.opener.document.getElementById('basicFactAmount' + index);
		parentBasicFactAmount.value = '0.00';
		
		window.close();
		
}

function openWinStorageInDetail(index, width, height, scroll) {
	var truelink = "selectStorage.action?rowIndex="+index;
	openWin(truelink, null, width, height, scroll);
}

/* 改变应收数量时，相应的基本单位应收数量做相应修改。 */
function getBasicShouldAmount(index) {
	
	getBasicAmount(index, "measureUnitId", "shouldAmount", "basicShouldAmount");

}

/* 改变实收数量时，相应的基本单位实收数量做相应修改,并且金额也做相应改变 */
function getBasicFactAmount(index) {
	
	var uPrice = document.getElementById("unitPrice" + index);
	var vUnitPrice = parseFloat(uPrice.value);
	var fAmount = document.getElementById("factAmount" + index);
	var vFactAmount = parseFloat(fAmount.value);
	var money = document.getElementById("money" + index);
	getBasicAmount(index, "measureUnitId", "factAmount", "basicFactAmount");
	if(vUnitPrice > 0.0 && vFactAmount > 0.0) {
		money.value = vUnitPrice * vFactAmount;
	}
}

function countDate(index) {


	var inStorageDate = document.getElementById("inStorageDate" + index);
	var vInStorageDate = inStorageDate.value;
	var inStoreLife = document.getElementById("inStoreLife" + index);
	var vInStoreLife = inStoreLife.value;
	var validationDate = document.getElementById("validationDate" + index);
	var vValidationDate = validationDate.value;
	
	var inStorageDateArray = inStorageDate.value.split("-");
	var iDate = new Date(inStorageDateArray[0], parseInt(inStorageDateArray[1]) - 1, inStorageDateArray[2]);
	if(vInStorageDate != null && !(trim(vInStorageDate) == "") && vInStoreLife != null && !(trim(vInStoreLife) == "")) {
		var vDate = new Date();
		vDate.setTime(iDate.getTime() + parseFloat(vInStoreLife) * 24 * 60 * 60 * 1000);
		validationDate.value = vDate.getFullYear() + "-" + (vDate.getMonth() + 1) + "-" + vDate.getDate();
	}

}



